<?php
class Admin_model_category extends Core_model_abstract
{
	public function __construct()
	{
		parent::__construct('category');
	}
	
	protected function _setFields()
	{
		return array(
			'id',
			'cateName',
			'pid',
			'cateKey',
			'keyWords',
			'desc',
			'urlName',
			'goodsQty',
			'createDate',
			'createPerson',
			'modifyDate',
			'modifyPerson'
		);
	}
	
	protected function _setPrimaryKeys()
	{
		return array(
			'id'
		);
	}
	
	protected function _setAutoIncrementFields()
	{
		return array(
			'id'
		);
	}
	
	protected function _setForeignKeyRelation()
	{
		return array();
	}
	
	public function add($cateData)
	{
		$cateData = $this->_getFieldsData($cateData);
		$cateKey = '0';
		if($cateData['pid'] > 0)
		{
			$dbSlave = Core_db_slave::getSlave();
			$pst = $dbSlave->query("SELECT `cateKey` FROM `{$this->_table}` WHERE `id`='{$cateData['pid']}'");
			$parent = $pst->fetch(PDO::FETCH_ASSOC);
			$cateKey = $parent['cateKey'].'-'.$cateData['pid'];
		}
		
		$cateData['cateKey'] = $cateKey;
		$cateData['createDate'] = time();
		
		return $this->_insert($cateData);
	}
	
	public function save($cateData)
	{
		$id = intval($cateData['id']);
		if($id < 1)
		{
			return false;
		}
		$cateData = $this->_getFieldsData($cateData);
		if($cateData['pid'] > 0)
		{
			$dbSlave = Core_db_slave::getSlave();
			$pst = $dbSlave->query("SELECT `cateKey` FROM `{$this->_table}` WHERE `id`='{$cateData['pid']}'");
			$parent = $pst->fetch(PDO::FETCH_ASSOC);
			$cateData['cateKey'] = $parent['cateKey'].'-'.$cateData['pid'];
		}
		$cateData['modifyDate'] = time();
		$dbMaster = Core_db_master::getMaster();
		$updateData = '';
		$dot = '';
		foreach($cateData as $key=>$value)
		{
			$updateData .= $dot."`{$key}`='{$value}'";
			$dot = ',';
		}
		$sql = "UPDATE `{$this->_table}` SET {$updateData} WHERE `id`='{$id}'";
		return $dbMaster->exec($sql);
	}
	
	public function delete($id)
	{
		if($this->hasChildren($id))
		{
			return false;
		}
		$sql = "DELETE FROM `{$this->_table}` WHERE `id`='{$id}'";
		return Core_db_master::getMaster()->exec($sql);
	}
	
	public function getOne($id)
	{
		$sql = "SELECT * FROM `{$this->_table}` WHERE `id`='{$id}'";
		$statement = Core_db_slave::getSlave()->query($sql);
		return $statement->fetch(PDO::FETCH_ASSOC);
	}
	
	public function hasChildren($id)
	{
		$sql = "SELECT * FROM `{$this->_table}` WHERE `pid`='{$id}'";
		$statement = Core_db_slave::getSlave()->query($sql);
		$result = $statement->fetch(PDO::FETCH_ASSOC);
		
		if(empty($result))
		{
			return false;
		}
		return true;
	}
	
	public function getAll($where = '')
	{
		if(!empty($where))
		{
			$where = " WHERE {$where}";
		}
		$dbSlave = Core_db_slave::getSlave();
		$sql = "SELECT *,CONCAT(`cateKey`, '-', `id`) AS `cateKeyID` FROM `{$this->_table}`$where ORDER BY `cateKeyID`";
		$statement = $dbSlave->query($sql);
		$result = $statement->fetchAll(PDO::FETCH_ASSOC);
		return $result;
	}
	
	/**
	 * 获取分类的树形结构
	 * @param string $where
	 * @return string 树形结构的html
	 */
	public function getTree($where = '', $itemLable = 'li', $groupLable = 'ul')
	{
		$data = $this->getAll($where);
		
		$treeHeml = '<ul>';
		
		$level = 1;
		
		$index = 0;
		
		foreach($data as $k=>$v)
		{
			$currentLevel = count(explode('-', $v['cateKey']));
			if(isset($data[$index + 1]))
			{
				$nextLevel = count(explode('-', $data[$index + 1]['cateKey']));
				$treeHeml .= "<li>{$v['cateName']}({$v['goodsQty']})";
				if($currentLevel > $nextLevel)
				{
					for($i = 0; $i < $currentLevel - $nextLevel; $i++)
					{
						$treeHeml .= '</li></ul></li>'; //回上级
					}
				}
				else if($currentLevel < $nextLevel)
				{
					$treeHeml .= '<ul>'; //往下级
				}
				else 
				{
					$treeHeml .= '</li>'; //同级
				}
			}
			else 
			{
				$treeHeml .= "<li>{$v['cateName']}({$v['goodsQty']})";
				for($i =0; $i < $currentLevel - 1; $i++)
				{
					$treeHeml .= '</ul></li>';
				}
			}
			
			$level = $currentLevel;
			$index++;
		}
		
		$treeHeml .= '</ul>';
		
		return $treeHeml;
	}

	protected function _validationField($fieldName, $value) 
	{
		return true;
	}

}